﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assembler
{
    public class Instruction
    {   
        //position constants for parsing input lines
        public const int LABEL_END_POS = 4;
        public const int OPCODE_END_POS = 8;
        public const int ADDR_END_POS = 12;
        public const int INDIRECT_POS = 13;
        public const int START_COMMENT_POS = 14;
        public const int INSTRUCTION_SIZE = 1;

        public const string EMPTY_STRING = "   ";

        public string label { get; set; }
        public string opcode { get; set; }
        //operand address
        public string refAddress { get; set; }
        public string comment { get; set; }
        public Boolean indirect { get; set; }
        public int instrAddress { get; set; }

        public Instruction(string opcode){
            this.opcode = opcode;

        }
        /**
         * Creates a new Instruction object from a line of assembly code
         **/
        public static Instruction parseInstruction(string assemLine){
            Instruction nextInstruction;
            //need to add whitespaces to set string to at least the starting comment spot
            for (int i = assemLine.Length; i < START_COMMENT_POS+1; i++)
            {
                assemLine += ' ';
            }
            string label = assemLine.Substring(0, 3);
            string opcode = assemLine.Substring(LABEL_END_POS + 1, 3);
            string addr = assemLine.Substring(OPCODE_END_POS + 1, 3);
            string comment = assemLine.Substring(START_COMMENT_POS, assemLine.Length - START_COMMENT_POS - 1);
            Boolean I = false;
            if (assemLine[INDIRECT_POS] == 'I')
            {
                I = true;
            }

            nextInstruction = new Instruction(opcode);
            nextInstruction.label = label;
            nextInstruction.comment = comment;
            nextInstruction.refAddress = addr;
            nextInstruction.indirect = I;
            return nextInstruction;
        }



    }


}
